

class ComicController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ comicInstanceList: Comic.list( params ), comicInstanceTotal: Comic.count() ]
    }

    def show = {
    	
    	println "id ${params.id}"
    	
        def comicInstance = Comic.get( params.id )

        if(!comicInstance) {
            flash.message = "Comic not found with id ${params.id}"
            redirect(action:list)
        }
        else {
        	Integer puntuacion = 0
        	comicInstance.ratings.each (){
				puntuacion += it.rate
				comicInstance.rating = puntuacion / comicInstance.ratings.size()
			}
        	
			println "puntu ${puntuacion}"
			
        	return render(controller:'comic', view:'_show', model:[ comicInstance : comicInstance ])
        }
    }

    def delete = {
        def comicInstance = Comic.get( params.id )
        if(comicInstance) {
            try {
                comicInstance.delete(flush:true)
                flash.message = "Comic ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Comic ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Comic not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def comicInstance = Comic.get( params.id )

        if(!comicInstance) {
            flash.message = "Comic not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ comicInstance : comicInstance ]
        }
    }

    def update = {
        def comicInstance = Comic.get( params.id )
        if(comicInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(comicInstance.version > version) {
                    
                    comicInstance.errors.rejectValue("version", "comic.optimistic.locking.failure", "Another user has updated this Comic while you were editing.")
                    render(view:'edit',model:[comicInstance:comicInstance])
                    return
                }
            }
            comicInstance.properties = params
            if(!comicInstance.hasErrors() && comicInstance.save()) {
                flash.message = "Comic ${params.id} updated"
                redirect(action:show,id:comicInstance.id)
            }
            else {
                render(view:'edit',model:[comicInstance:comicInstance])
            }
        }
        else {
            flash.message = "Comic not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def comicInstance = new Comic()
        comicInstance.properties = params
        return ['comicInstance':comicInstance]
    }

    def save = {
        def comicInstance = new Comic(params)
        if(!comicInstance.hasErrors() && comicInstance.save()) {
            flash.message = "Comic ${comicInstance.id} created"
            redirect(action:show,id:comicInstance.id)
        }
        else {
            render(view:'create',model:[comicInstance:comicInstance])
        }
    }
}
